/**
 * 客户名称更新后，更新科室名称和专业组名称
 * created by Harry 2016-12-09 15:30:58
 */
/**
 * 客户OwnerId变更后，更新客户相关的联系人,合同,活动的Owner
 * created by Harry 2017-7-19 15:30:58
 */
public class AccUpdateDeptNameHandler implements Triggers.handler {
    public void handle(){
        List<Account> newAcc = (List<Account>) Trigger.new;
        Map<Id,Account> oldAcc = (Map<Id,Account>) Trigger.oldMap;
        Set<Id> accIdSet = new Set<Id>();
        //保存客户的所有者
        Map<Id,Id>owners = new Map<Id,Id>();
        //变更所有者的客户Id
        Set<Id> accOwnerSet = new Set<Id>();
        
        for ( Account acc : newAcc){
            Account old = oldAcc.get(acc.Id);
            if(old.Name!=acc.Name){
                accIdSet.add(acc.Id);
            }
            //保存ownerId
            if(old.ownerId != acc.ownerId){
            	owners.put(acc.Id,acc.ownerId);
            	accOwnerSet.add(acc.Id);
            }
            
        }
        Map<Id,Boolean>ownerisActive = new Map<Id,Boolean>();
        for(User u:[select Id,isActive from User where  Id in: owners.values()]){
        ownerisActive.put(u.Id,u.isActive);
        }
        
        if (accIdSet.size() > 0) {
            List<Department__c> deptList = [select Name,TerminalCustomer__r.Name, hospitalDepartments__c from Department__c where TerminalCustomer__c in : accIdSet];
            for (Department__c dept : deptList){
                dept.Name = dept.TerminalCustomer__r.Name+dept.hospitalDepartments__c;
            }
            System.debug(deptList);
            Database.update(deptList);
            
            //顺便修改了专业组的名称
            List<SpecialTeam__c> specList = [select Name,Team2Dept__r.Name,TeamType__c from SpecialTeam__c where TerminalCustomer__c in :accIdSet];
            for(SpecialTeam__c spec : specList){
                spec.Name = spec.Team2Dept__r.Name+spec.TeamType__c;
            }
            Database.update(specList);
        }
        if(accOwnerSet.size() > 0){
        //更新联系人的OwnerId
            List<Contact> contacts = [select Id,OwnerId,AccountId from Contact where AccountId in:accOwnerSet];
            for(Contact c :contacts){
             if(owners.containsKey(c.AccountId) && ownerisActive.containsKey(owners.get(c.AccountId))  && ownerisActive.get(owners.get(c.AccountId)) == true )c.ownerId = owners.get(c.AccountId);
            }
             Database.update(contacts);
        }
            
        /*
        //更新合同的所有人
        List<Contract__c> contracts = [select Id,OwnerId,Account__c from Contract__c where Account__c in:accOwnerSet];
        for(Contract__c c :contracts){
         if(owners.containsKey(c.Account__c))c.ownerId = owners.get(c.Account__c);
        }
         Database.update(contracts);
       // 同时把活动的所有人也改了
List<Event> eList = [select Id,Account__c,ownerId from Event where Account__c in :accOwnerSet ];
for (Event e : eList) {
         if(owners.containsKey(e.Account__c))e.ownerId = owners.get(e.Account__c);
}
         Database.update(eList);
List<Task> tList = [select Id,Account__c,ownerId from Task where Account__c in :accOwnerSet ];
for (Task t : tList) {
         if(owners.containsKey(t.Account__c))t.ownerId = owners.get(t.Account__c);
}
                 Database.update(tList);

//将业务机会的所有人也修改
        List<Opportunity> oList = [select Id,AccountId,ownerId from Opportunity where AccountId in :accOwnerSet ];
        for(Opportunity o : oList){
         if(owners.containsKey(o.AccountId))o.ownerId = owners.get(o.AccountId);
         } 
                 Database.update(oList);
                 */

                    
    }
}